package com.uxwine.alarm.more;

import java.util.Calendar;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.uxwine.alarm.MainActivity;
import com.uxwine.alarm.R;
import com.uxwine.alarm.R.anim;
import com.uxwine.alarm.R.id;
import com.uxwine.alarm.R.layout;
import com.uxwine.alarm.main.Alarm;
import com.uxwine.alarm.main.ClockActivity;
import com.uxwine.alarm.main.DBAlarms;
import com.uxwine.cmm.BaseFragment;

public class MoreFragment extends BaseFragment implements OnTouchListener {
	private View mMainView;
	ListView mlvAlarms;
	List<Alarm> mAlarms;
	LayoutInflater mInflater;
	MAdpater mAdapter;
	MainActivity mAct;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mAdapter = new MAdpater();
		mAct = (MainActivity) getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		mMainView = inflater.inflate(R.layout.fragment_more, container, false);
		mlvAlarms = (ListView) mMainView.findViewById(R.id.main_lv_alarmlist);
		mlvAlarms.setAdapter(mAdapter);
		mlvAlarms.setOnItemClickListener(mLVListener);
		ImageView img = (ImageView) mMainView
				.findViewById(R.id.main_img_alarmlist_empty);
		mlvAlarms.setEmptyView(img);
		mlvAlarms.setOnTouchListener(this);
		mlvAlarms.setLayoutAnimation(getListAnim());
		// loadAlarms();
		return mMainView;
	}

	public void loadAlarms() {
		DBAlarms db = new DBAlarms(getActivity(), false);
		mAlarms = db.getAll();
		db.close();
		mAdapter.alarms = mAlarms;
		mAdapter.notifyDataSetChanged();
	}

	private void editAlarm(Alarm a) {
		mAct.showMain(a);
	}

	private AdapterView.OnItemClickListener mLVListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> av, View v, int p, long value) {
			if (isFling) {
				return;
			}
			Alarm a = (Alarm) v.getTag();
			if (null != a)
				editAlarm(a);
		}
	};

	protected class MAdpater extends BaseAdapter {
		List<Alarm> alarms;

		@Override
		public int getCount() {
			if (alarms != null) {
				return alarms.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int p) {
			if (null != alarms)
				return alarms.get(p);
			return null;
		}

		@Override
		public long getItemId(int p) {
			return p;
		}

		@Override
		public View getView(int position, View v, ViewGroup root) {
			v = mInflater.inflate(R.layout.listitem_alarm, null);
			LinearLayout layout = (LinearLayout) v
					.findViewById(R.id.li_alarm_rtv);
			ImageView imgDivider = (ImageView) v
					.findViewById(R.id.li_alarm_img_state);
			TextView tvName = (TextView) v.findViewById(R.id.li_alarm_tv_clock);
			TextView tvBrief = (TextView) v
					.findViewById(R.id.li_alarm_tv_brief);

			Alarm a = (Alarm) getItem(position);
			tvName.setText(a.getName());

			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(a.getTime());
			tvBrief.setText(c.getTime().toLocaleString());

			if (!a.isEffective()) {
				layout.setBackgroundResource(R.drawable.alarm_listitem_bg_gray);
			} else {
				int i = position % 3;
				if (i == 0) {
					imgDivider
							.setImageResource(R.drawable.alarm_listitem_running_green);
					layout.setBackgroundResource(R.drawable.alarm_listitem_bg_green);
				} else if (i == 1) {
					imgDivider
							.setImageResource(R.drawable.alarm_listitem_running_palegreen);
					layout.setBackgroundResource(R.drawable.alarm_listitem_bg_palegreen);
				} else if (i == 2) {
					imgDivider
							.setImageResource(R.drawable.alarm_listitem_running_yellow);
					layout.setBackgroundResource(R.drawable.alarm_listitem_bg_yellow);
				}
			}

			v.setTag(a);
			return v;
		}
	}

	float x, y, upx, upy;
	boolean isFling;

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		isFling = false;
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			x = event.getX();
			y = event.getY();
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			upx = event.getX();
			upy = event.getY();
			int position1 = ((ListView) v).pointToPosition((int) x, (int) y);
			int position2 = ((ListView) v)
					.pointToPosition((int) upx, (int) upy);

			if (position1 == position2 && Math.abs(x - upx) > 50) {
				int firstVisiblePosition = mlvAlarms.getFirstVisiblePosition();
				View view = ((ListView) v).getChildAt(position1
						- firstVisiblePosition);
				if (view == null) {
					Toast.makeText(mAct, position1 + "", Toast.LENGTH_SHORT)
							.show();
				}
				isFling = true;
				if (x < upx) {
					updateListItem(view, position1, false);
				} else {
					updateListItem(view, position1, true);
				}
			}
		}
		return false;
	}

	private void updateListItem(final View rowView, final int positon,
			final boolean flag) {		
		if (rowView == null) {
			return;
		}
		final Animation animation = (Animation) AnimationUtils.loadAnimation(
				rowView.getContext(), flag ? R.anim.item_anim_left
						: R.anim.item_anim_right);
		animation.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {
				if (flag) {
					deleteAlarm((Alarm) rowView.getTag(), positon);
				} else {
					setAlarmPause((Alarm) rowView.getTag(), positon);
				}
				animation.cancel();
			}
		});
		rowView.startAnimation(animation);
	}

	private void deleteAlarm(Alarm alarm, int position) {
		DBAlarms db = new DBAlarms(getActivity(), false);
		db.delete(alarm.getId());
		db.close();

		mAlarms.remove(position);
		mAdapter.alarms = mAlarms;
		mAdapter.notifyDataSetChanged();
	}

	private void setAlarmPause(Alarm alarm, int position) {		
		alarm.setEffective(!alarm.isEffective());
		DBAlarms db = new DBAlarms(getActivity(), false);
		db.update(alarm);
		db.close();
		if(!alarm.isEffective()){
			mAlarms.remove(position);
			mAlarms.add(alarm);
		}else{
			
		}
	
		mAdapter.alarms = mAlarms;
		mAdapter.notifyDataSetChanged();
	}

	private LayoutAnimationController getListAnim() {
		AnimationSet set = new AnimationSet(true);
		Animation animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(300);
		set.addAnimation(animation);

		animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		animation.setDuration(500);
		set.addAnimation(animation);
		LayoutAnimationController controller = new LayoutAnimationController(
				set, 0.5f);
		return controller;
	}
}
